package br.inatel.ec203.projeto.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class ComponentComboBoxPanel extends JPanel{
	
	private static final long serialVersionUID = -3082884989803504237L;
	
	private JLabel componentLabel;
	private JComboBox<String> componentComboBox;

	private GridBagConstraints componentLabelConstraints;
	private GridBagConstraints componentComboBoxConstraints;
	
	private ArrayList<ComponentComboBoxListeners> ComponentComboBoxListeners = new ArrayList<ComponentComboBoxListeners>();
	
	public ComponentComboBoxPanel () {
		
		initialize();
	}

	private void initialize() {
		setLayout(new GridBagLayout());
		
		add(getComponentLabel(), getComponentLabelConstraints());
		add(getComponentComboBox(), getComponentComboBoxConstraints());
	}
	
	private JLabel getComponentLabel() {
		if (componentLabel == null){
			componentLabel = new JLabel();
			componentLabel.setText("Componente");
		}
		return componentLabel;
	}

	public JComboBox<String> getComponentComboBox() {
		if (componentComboBox == null){
			componentComboBox = new JComboBox<String>();
			componentComboBox.addItem(" ");
			componentComboBox.addItem("Resistor");
			componentComboBox.addItem("Capacitor");
			componentComboBox.addItem("CI");
			componentComboBox.addItem("Transistor");
			componentComboBox.addItem("Outros");
			componentComboBox.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {					
					int selectedIndex = componentComboBox.getSelectedIndex();
					if (selectedIndex == 0) {
						for (ComponentComboBoxListeners listener : ComponentComboBoxListeners) {
							listener.vazioPerformed();
						}
					}
					if (selectedIndex == 1) {
						for (ComponentComboBoxListeners listener : ComponentComboBoxListeners) {
							listener.resistorPerformed();
						}
					}
					else if (selectedIndex == 2) {
						for (ComponentComboBoxListeners listener : ComponentComboBoxListeners) {
							listener.capacitorPerformed();
						}
					}
					else if (selectedIndex == 3) {
						for (ComponentComboBoxListeners listener : ComponentComboBoxListeners) {
							listener.CiPerformed();
						}
					}
					else if (selectedIndex == 4) {
						for (ComponentComboBoxListeners listener : ComponentComboBoxListeners) {
							listener.transistorPerformed();
						}
					}
					else if (selectedIndex == 5) {
						for (ComponentComboBoxListeners listener : ComponentComboBoxListeners) {
							listener.outrosPerformed();
						}
					}
				}
			});
		}
		return componentComboBox;
	}
	
	@SuppressWarnings("static-access")
	private GridBagConstraints getComponentLabelConstraints() {
		if (componentLabelConstraints == null) {
			componentLabelConstraints = new GridBagConstraints();
			componentLabelConstraints.gridx = 0;
			componentLabelConstraints.gridy = 1;
			componentLabelConstraints.insets = new Insets(2, 2, 2, 2);
			componentLabelConstraints.anchor = new GridBagConstraints().LINE_START;
		}
		return componentLabelConstraints;
	}

	@SuppressWarnings("static-access")
	private GridBagConstraints getComponentComboBoxConstraints() {
		if (componentComboBoxConstraints == null) {
			componentComboBoxConstraints = new GridBagConstraints();
			componentComboBoxConstraints.gridx = 1;
			componentComboBoxConstraints.gridy = 1;
			componentComboBoxConstraints.gridwidth = 3;
			componentComboBoxConstraints.fill = new GridBagConstraints().HORIZONTAL;
			componentComboBoxConstraints.insets = new Insets(3, 3, 3, 3);
		}
		return componentComboBoxConstraints;
	}
	
	public void addComponentComboBoxListeners(ComponentComboBoxListeners componentcomboBoxListener) {
		ComponentComboBoxListeners.add(componentcomboBoxListener);
	}
}
